package com.fin.zw.intelligentbot.controller;

import com.fin.zw.intelligentbot.annotation.CurrentUser;
import com.fin.zw.intelligentbot.entity.Blacklist;
import com.fin.zw.intelligentbot.model.session.SessionModel;
import com.fin.zw.intelligentbot.service.BlacklistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/blacklist")
@Api(value = "黑名单信息管理", description = "黑名单信息管理")
public class BlacklistController {

    @Autowired
    private BlacklistService blacklistService;


    @GetMapping("/fetchPage")
    @ApiOperation(value = "查询所有的黑名单信息", notes = "查询所有的黑名单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<Blacklist>> queryBlacklist(Pageable pageable) {
        Page<Blacklist> page = blacklistService.queryBlacklist(pageable);
        return ResponseEntity.ok().body(page);
    }


    @PostMapping("/add")
    @ApiOperation(value = "添加黑名单信息", notes = "添加黑名单信息")
    public ResponseEntity<String> createBlacklist(@RequestParam("phone") String phone,
                                                  @ApiIgnore @CurrentUser SessionModel sessionModel) {
        blacklistService.createBlacklist(phone, sessionModel);
        return ResponseEntity.ok().body("操作成功");
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "黑名单信息删除", notes = "黑名单信息删除")
    public ResponseEntity<String> deleteBlacklistById(@PathVariable String id) {
        blacklistService.deleteBlacklistById(id);
        return ResponseEntity.ok().body("操作成功");
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "黑名单信息删除", notes = "黑名单信息删除")
    public ResponseEntity<String> deleteBlacklistByPhone(@RequestParam("phone") String phone) {
        blacklistService.deleteBlacklistByPhone(phone);
        return ResponseEntity.ok().body("操作成功");
    }
}
